package com.websem.client.views;

import java.util.HashMap;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.Widget;
import com.websem.client.services.FilmService;
import com.websem.client.services.FilmServiceAsync;

public class MainView extends Composite {

	private DockPanel mainPanel = new DockPanel();
	private final FilmServiceAsync filmService = GWT.create(FilmService.class);
	private AbsolutePanel north1 = new AbsolutePanel();
	private AbsolutePanel north2 = new AbsolutePanel();
	private AbsolutePanel center = new AbsolutePanel();
	private ScrollPanel scrollPanel = new ScrollPanel(center);

	public AbsolutePanel getCenter() {
		return center;
	}

	private AbsolutePanel south1 = new AbsolutePanel();
	private AbsolutePanel south2 = new AbsolutePanel();
	private AbsolutePanel east = new AbsolutePanel();
	private AbsolutePanel west = new AbsolutePanel();

	public MainView() {
		super();
		mainPanel.setHeight("100%");
		mainPanel.getElement().getStyle().setProperty("border", "none");
		// north1.getElement().getStyle().setProperty("border",
		// "solid red 1px");
		// north2.getElement().getStyle().setProperty("border",
		// "solid red 1px");
//		 center.getElement().getStyle().setProperty("border",
//		 "solid red 1px");
		// south1.getElement().getStyle().setProperty("border",
		// "solid red 1px");
		// south2.getElement().getStyle().setProperty("border",
		// "solid red 1px");
		// east.getElement().getStyle().setProperty("border", "solid red 1px");
		// west.getElement().getStyle().setProperty("border", "solid red 1px");
	}

	public Widget onInitialize() {
		mainPanel.setWidth("100%");
		mainPanel.setSpacing(0);
		mainPanel.setHorizontalAlignment(DockPanel.ALIGN_CENTER);

		north1.getElement().setAttribute("border", "none");
		// north1.setStyleName("mainpaneltd");
		north1.setHeight("50px");

		north2.getElement().setAttribute("id", "north2");
		north2.getElement().getStyle().setProperty("overflow", "visible");

		north2.getElement().getStyle().setProperty("margin", "5px");

		south1.getElement().setAttribute("id", "south1");
		// south1.setStyleName("mainpaneltd");
		south1.setHeight("50px");

		south2.getElement().setAttribute("id", "south2");
		south2.setStyleName("mainpaneltd");
		south2.setHeight("75px");

		center.getElement().setAttribute("id", "center");
		// center.setStyleName("mainpaneltd");
		center.setHeight("600px");
		center.setWidth("1050px");
		center.getElement().getStyle().setProperty("overflow", "visible");
		east.getElement().setAttribute("id", "east");
		// east.setStyleName("mainpaneltd");
		east.setHeight("450px");
		east.setWidth("100px");

		west.getElement().setAttribute("id", "west");
		west.setStyleName("mainpaneltd");
		west.setHeight("450px");
		west.setWidth("100px");

		mainPanel.add(north1, DockPanel.NORTH);
		mainPanel.add(south1, DockPanel.SOUTH);
		mainPanel.add(east, DockPanel.EAST);
		mainPanel.add(west, DockPanel.WEST);
		mainPanel.add(scrollPanel, DockPanel.CENTER);
		mainPanel.add(north2, DockPanel.NORTH);
		mainPanel.add(south2, DockPanel.SOUTH);

		mainPanel.ensureDebugId("cwDockPanel");

		
		final TitleView tv = new TitleView();
		north1.add(tv.onInitialize());
		// Initialization : coming soon films
				center.clear();
				center.add(new Image("images/comingSoon-logo.png"));
				filmService
						.getComingSoonFilm(new AsyncCallback<List<HashMap<String, String>>>() {

							@Override
							public void onSuccess(List<HashMap<String, String>> result) {
								for (HashMap<String, String> film : result) {
									center.add(new SearchResultView(film
											.get("filmName"), film.get("year"), "["
											+ film.get("version") + "]", new String(
											film.get("actor0") + ", "
													+ film.get("actor1") + ", "
													+ film.get("actor2")), film
											.get("producerName"), film
											.get("directorName"), film.get("date"),
											film.get("duration"), film.get("synopsis"),
											film.get("affiche"), film.get("rating")
													+ "/10"));
								}

							}

							@Override
							public void onFailure(Throwable caught) {
								// TODO Auto-generated method stub

							}
						});

				// end ini

		tv.getNormalPushButton().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				filmService.getFilmByWord(tv.getNormalText().getText(),
						new AsyncCallback<List<HashMap<String, String>>>() {

							@Override
							public void onSuccess(
									List<HashMap<String, String>> result) {
								center.clear();
								for (HashMap<String, String> film : result) {
									GWT.log("Film Name: "
											+ film.get("filmName"));
									center.add(new SearchResultView(
											film.get("filmName"),
											film.get("year"),
											"[" + film.get("version") + "]",
											new String(film.get("actor0")
													+ ", " + film.get("actor1")
													+ ", " + film.get("actor2")),
											film.get("producerName"), film
													.get("directorName"), film
													.get("date"), film
													.get("duration"), film
													.get("synopsis"), film
													.get("affiche"), film
													.get("rating") + "/10"));
								}

							}

							@Override
							public void onFailure(Throwable caught) {
								GWT.log("Callback failed when executing query");
							}
						});

			}
		});

		// zone centrale
		final SearchBarView sv = new SearchBarView();

		// search Queries

		// search by genre
		sv.getGenreListBox().addChangeHandler(new ChangeHandler() {

			@Override
			public void onChange(ChangeEvent event) {
				System.out.println(sv.getSelectedCriterias());
				if (sv.getSelectedCriterias().substring(1, 6)
						.equalsIgnoreCase("00000")) {
					filmService.getFilmByGenre(
							sv.getGenreListBox().getItemText(
									sv.getGenreListBox().getSelectedIndex()),
							new AsyncCallback<List<HashMap<String, String>>>() {
								@Override
								public void onSuccess(
										List<HashMap<String, String>> result) {
									center.clear();
									for (HashMap<String, String> film : result) {
										GWT.log("Film Name: "
												+ film.get("filmName"));
										center.add(new SearchResultView(film
												.get("filmName"), film
												.get("year"), "["
												+ film.get("version") + "]",
												new String(film.get("actor0")
														+ ", "
														+ film.get("actor1")
														+ ", "
														+ film.get("actor2")),
												film.get("producerName"), film
														.get("directorName"),
												film.get("date"), film
														.get("duration"), film
														.get("synopsis"), film
														.get("affiche"), film
														.get("rating") + "/10"));
									}
								}

								@Override
								public void onFailure(Throwable caught) {
									GWT.log("Callback failed when executing query");
								}
							});
				} else {
					filmService
							.findFilms(
									sv.getGenreListBox().getItemText(
											sv.getGenreListBox()
													.getSelectedIndex()),
									sv.getCountryListBox().getItemText(
											sv.getCountryListBox()
													.getSelectedIndex()),
									sv.getPrizeListBox().getItemText(
											sv.getPrizeListBox()
													.getSelectedIndex()),
									sv.getLanguageListBox().getItemText(
											sv.getLanguageListBox()
													.getSelectedIndex()),
									sv.getYearListBox().getItemText(
											sv.getYearListBox()
													.getSelectedIndex()),
									sv.getVersionListBox().getItemText(
											sv.getVersionListBox()
													.getSelectedIndex()),
									new AsyncCallback<List<HashMap<String, String>>>() {

										@Override
										public void onFailure(Throwable caught) {
											// TODO Auto-generated method stub

										}

										@Override
										public void onSuccess(
												List<HashMap<String, String>> result) {
											center.clear();
											for (HashMap<String, String> film : result) {
												center.add(new SearchResultView(
														film.get("filmName"),
														film.get("year"),
														"["
																+ film.get("version")
																+ "]",
														new String(
																film.get("actor0")
																		+ ", "
																		+ film.get("actor1")
																		+ ", "
																		+ film.get("actor2")),
														film.get("producerName"),
														film.get("directorName"),
														film.get("date"),
														film.get("duration"),
														film.get("synopsis"),
														film.get("affiche"),
														film.get("rating")
																+ "/10"));

											}
										}
									});
				}

			}
		});
		// search by country
		sv.getCountryListBox().addChangeHandler(new ChangeHandler() {

			@Override
			public void onChange(ChangeEvent event) {
				System.out.println(sv.getSelectedCriterias());
				if (sv.getSelectedCriterias().substring(0, 1)
						.equalsIgnoreCase("0")
						&& sv.getSelectedCriterias().substring(2, 6)
								.equalsIgnoreCase("0000")) {
					filmService.getFilmByCountry(
							sv.getCountryListBox().getItemText(
									sv.getCountryListBox().getSelectedIndex()),
							new AsyncCallback<List<HashMap<String, String>>>() {
								@Override
								public void onSuccess(
										List<HashMap<String, String>> result) {
									center.clear();
									for (HashMap<String, String> film : result) {
										GWT.log("Film Name: "
												+ film.get("filmName"));
										center.add(new SearchResultView(film
												.get("filmName"), film
												.get("year"), "["
												+ film.get("version") + "]",
												new String(film.get("actor0")
														+ ", "
														+ film.get("actor1")
														+ ", "
														+ film.get("actor2")),
												film.get("producerName"), film
														.get("directorName"),
												film.get("date"), film
														.get("duration"), film
														.get("synopsis"), film
														.get("affiche"), film
														.get("rating") + "/10"));
									}
								}

								@Override
								public void onFailure(Throwable caught) {
									GWT.log("Callback failed when executing query");
								}
							});
				}

				else {
					filmService
							.findFilms(
									sv.getGenreListBox().getItemText(
											sv.getGenreListBox()
													.getSelectedIndex()),
									sv.getCountryListBox().getItemText(
											sv.getCountryListBox()
													.getSelectedIndex()),
									sv.getPrizeListBox().getItemText(
											sv.getPrizeListBox()
													.getSelectedIndex()),
									sv.getLanguageListBox().getItemText(
											sv.getLanguageListBox()
													.getSelectedIndex()),
									sv.getYearListBox().getItemText(
											sv.getYearListBox()
													.getSelectedIndex()),
									sv.getVersionListBox().getItemText(
											sv.getVersionListBox()
													.getSelectedIndex()),
									new AsyncCallback<List<HashMap<String, String>>>() {

										@Override
										public void onFailure(Throwable caught) {
											// TODO Auto-generated method stub

										}

										@Override
										public void onSuccess(
												List<HashMap<String, String>> result) {
											center.clear();
											for (HashMap<String, String> film : result) {
												center.add(new SearchResultView(
														film.get("filmName"),
														film.get("year"),
														"["
																+ film.get("version")
																+ "]",
														new String(
																film.get("actor0")
																		+ ", "
																		+ film.get("actor1")
																		+ ", "
																		+ film.get("actor2")),
														film.get("producerName"),
														film.get("directorName"),
														film.get("date"),
														film.get("duration"),
														film.get("synopsis"),
														film.get("affiche"),
														film.get("rating")
																+ "/10"));

											}
										}
									});

				}

			}
		});

		// search by prize
		sv.getPrizeListBox().addChangeHandler(new ChangeHandler() {

			@Override
			public void onChange(ChangeEvent event) {
				System.out.println(sv.getSelectedCriterias());
				if (sv.getSelectedCriterias().substring(0, 2)
						.equalsIgnoreCase("00")
						&& sv.getSelectedCriterias().substring(3, 6)
								.equalsIgnoreCase("000")) {
					filmService.getFilmByPrize(
							sv.getPrizeListBox().getItemText(
									sv.getPrizeListBox().getSelectedIndex()),
							new AsyncCallback<List<HashMap<String, String>>>() {
								@Override
								public void onSuccess(
										List<HashMap<String, String>> result) {
									center.clear();
									for (HashMap<String, String> film : result) {
										GWT.log("Film Name: "
												+ film.get("filmName"));
										center.add(new SearchResultView(film
												.get("filmName"), film
												.get("year"), "["
												+ film.get("version") + "]",
												new String(film.get("actor0")
														+ ", "
														+ film.get("actor1")
														+ ", "
														+ film.get("actor2")),
												film.get("producerName"), film
														.get("directorName"),
												film.get("date"), film
														.get("duration"), film
														.get("synopsis"), film
														.get("affiche"), film
														.get("rating") + "/10"));
									}
								}

								@Override
								public void onFailure(Throwable caught) {
									GWT.log("Callback failed when executing query");
								}
							});
				} else {
					filmService
							.findFilms(
									sv.getGenreListBox().getItemText(
											sv.getGenreListBox()
													.getSelectedIndex()),
									sv.getCountryListBox().getItemText(
											sv.getCountryListBox()
													.getSelectedIndex()),
									sv.getPrizeListBox().getItemText(
											sv.getPrizeListBox()
													.getSelectedIndex()),
									sv.getLanguageListBox().getItemText(
											sv.getLanguageListBox()
													.getSelectedIndex()),
									sv.getYearListBox().getItemText(
											sv.getYearListBox()
													.getSelectedIndex()),
									sv.getVersionListBox().getItemText(
											sv.getVersionListBox()
													.getSelectedIndex()),
									new AsyncCallback<List<HashMap<String, String>>>() {

										@Override
										public void onFailure(Throwable caught) {
											// TODO Auto-generated method stub

										}

										@Override
										public void onSuccess(
												List<HashMap<String, String>> result) {
											center.clear();
											for (HashMap<String, String> film : result) {
												center.add(new SearchResultView(
														film.get("filmName"),
														film.get("year"),
														"["
																+ film.get("version")
																+ "]",
														new String(
																film.get("actor0")
																		+ ", "
																		+ film.get("actor1")
																		+ ", "
																		+ film.get("actor2")),
														film.get("producerName"),
														film.get("directorName"),
														film.get("date"),
														film.get("duration"),
														film.get("synopsis"),
														film.get("affiche"),
														film.get("rating")
																+ "/10"));

											}
										}
									});
				}

			}
		});

		// search by language
		sv.getLanguageListBox().addChangeHandler(new ChangeHandler() {

			@Override
			public void onChange(ChangeEvent event) {
				if (sv.getSelectedCriterias().substring(0, 3)
						.equalsIgnoreCase("000")
						&& sv.getSelectedCriterias().substring(4, 6)
								.equalsIgnoreCase("00")) {
					filmService
							.getFilmByLanguage(
									sv.getLanguageListBox().getItemText(
											sv.getLanguageListBox()
													.getSelectedIndex()),
									new AsyncCallback<List<HashMap<String, String>>>() {
										@Override
										public void onSuccess(
												List<HashMap<String, String>> result) {
											center.clear();
											for (HashMap<String, String> film : result) {
												GWT.log("Film Name: "
														+ film.get("filmName"));
												center.add(new SearchResultView(
														film.get("filmName"),
														film.get("year"),
														"["
																+ film.get("version")
																+ "]",
														new String(
																film.get("actor0")
																		+ ", "
																		+ film.get("actor1")
																		+ ", "
																		+ film.get("actor2")),
														film.get("producerName"),
														film.get("directorName"),
														film.get("date"),
														film.get("duration"),
														film.get("synopsis"),
														film.get("affiche"),
														film.get("rating")
																+ "/10"));
											}
										}

										@Override
										public void onFailure(Throwable caught) {
											GWT.log("Callback failed when executing query");
										}
									});
				} else {
					filmService
							.findFilms(
									sv.getGenreListBox().getItemText(
											sv.getGenreListBox()
													.getSelectedIndex()),
									sv.getCountryListBox().getItemText(
											sv.getCountryListBox()
													.getSelectedIndex()),
									sv.getPrizeListBox().getItemText(
											sv.getPrizeListBox()
													.getSelectedIndex()),
									sv.getLanguageListBox().getItemText(
											sv.getLanguageListBox()
													.getSelectedIndex()),
									sv.getYearListBox().getItemText(
											sv.getYearListBox()
													.getSelectedIndex()),
									sv.getVersionListBox().getItemText(
											sv.getVersionListBox()
													.getSelectedIndex()),
									new AsyncCallback<List<HashMap<String, String>>>() {

										@Override
										public void onFailure(Throwable caught) {
											// TODO Auto-generated method stub

										}

										@Override
										public void onSuccess(
												List<HashMap<String, String>> result) {
											center.clear();
											for (HashMap<String, String> film : result) {
												center.add(new SearchResultView(
														film.get("filmName"),
														film.get("year"),
														"["
																+ film.get("version")
																+ "]",
														new String(
																film.get("actor0")
																		+ ", "
																		+ film.get("actor1")
																		+ ", "
																		+ film.get("actor2")),
														film.get("producerName"),
														film.get("directorName"),
														film.get("date"),
														film.get("duration"),
														film.get("synopsis"),
														film.get("affiche"),
														film.get("rating")
																+ "/10"));

											}
										}
									});
				}

			}
		});

		// search by date
		sv.getYearListBox().addChangeHandler(new ChangeHandler() {

			@Override
			public void onChange(ChangeEvent event) {
				if (sv.getSelectedCriterias().substring(0, 4)
						.equalsIgnoreCase("0000")
						&& sv.getSelectedCriterias().substring(5, 6)
								.equalsIgnoreCase("0")) {
					filmService.getFilmByDate(
							sv.getYearListBox().getItemText(
									sv.getYearListBox().getSelectedIndex()),
							new AsyncCallback<List<HashMap<String, String>>>() {
								@Override
								public void onSuccess(
										List<HashMap<String, String>> result) {
									center.clear();
									for (HashMap<String, String> film : result) {
										GWT.log("Film Name: "
												+ film.get("filmName"));
										center.add(new SearchResultView(film
												.get("filmName"), film
												.get("year"), "["
												+ film.get("version") + "]",
												new String(film.get("actor0")
														+ ", "
														+ film.get("actor1")
														+ ", "
														+ film.get("actor2")),
												film.get("producerName"), film
														.get("directorName"),
												film.get("date"), film
														.get("duration"), film
														.get("synopsis"), film
														.get("affiche"), film
														.get("rating") + "/10"));
									}
								}

								@Override
								public void onFailure(Throwable caught) {
									GWT.log("Callback failed when executing query");
								}
							});
				} else {
					filmService
							.findFilms(
									sv.getGenreListBox().getItemText(
											sv.getGenreListBox()
													.getSelectedIndex()),
									sv.getCountryListBox().getItemText(
											sv.getCountryListBox()
													.getSelectedIndex()),
									sv.getPrizeListBox().getItemText(
											sv.getPrizeListBox()
													.getSelectedIndex()),
									sv.getLanguageListBox().getItemText(
											sv.getLanguageListBox()
													.getSelectedIndex()),
									sv.getYearListBox().getItemText(
											sv.getYearListBox()
													.getSelectedIndex()),
									sv.getVersionListBox().getItemText(
											sv.getVersionListBox()
													.getSelectedIndex()),
									new AsyncCallback<List<HashMap<String, String>>>() {

										@Override
										public void onFailure(Throwable caught) {
											// TODO Auto-generated method stub

										}

										@Override
										public void onSuccess(
												List<HashMap<String, String>> result) {
											center.clear();
											for (HashMap<String, String> film : result) {
												center.add(new SearchResultView(
														film.get("filmName"),
														film.get("year"),
														"["
																+ film.get("version")
																+ "]",
														new String(
																film.get("actor0")
																		+ ", "
																		+ film.get("actor1")
																		+ ", "
																		+ film.get("actor2")),
														film.get("producerName"),
														film.get("directorName"),
														film.get("date"),
														film.get("duration"),
														film.get("synopsis"),
														film.get("affiche"),
														film.get("rating")
																+ "/10"));

											}
										}
									});

				}

			}
		});

		// search by version
		sv.getVersionListBox().addChangeHandler(new ChangeHandler() {

			@Override
			public void onChange(ChangeEvent event) {
				if (sv.getSelectedCriterias().substring(0, 5)
						.equalsIgnoreCase("00000")) {
					filmService.getFilmByVersion(
							sv.getVersionListBox().getItemText(
									sv.getVersionListBox().getSelectedIndex()),
							new AsyncCallback<List<HashMap<String, String>>>() {
								@Override
								public void onSuccess(
										List<HashMap<String, String>> result) {
									center.clear();
									for (HashMap<String, String> film : result) {
										GWT.log("Film Name: "
												+ film.get("filmName"));
										center.add(new SearchResultView(film
												.get("filmName"), film
												.get("year"), "["
												+ film.get("version") + "]",
												new String(film.get("actor0")
														+ ", "
														+ film.get("actor1")
														+ ", "
														+ film.get("actor2")),
												film.get("producerName"), film
														.get("directorName"),
												film.get("date"), film
														.get("duration"), film
														.get("synopsis"), film
														.get("affiche"), film
														.get("rating") + "/10"));
									}
								}

								@Override
								public void onFailure(Throwable caught) {
									GWT.log("Callback failed when executing query");
								}
							});
				} else {
					filmService
							.findFilms(
									sv.getGenreListBox().getItemText(
											sv.getGenreListBox()
													.getSelectedIndex()),
									sv.getCountryListBox().getItemText(
											sv.getCountryListBox()
													.getSelectedIndex()),
									sv.getPrizeListBox().getItemText(
											sv.getPrizeListBox()
													.getSelectedIndex()),
									sv.getLanguageListBox().getItemText(
											sv.getLanguageListBox()
													.getSelectedIndex()),
									sv.getYearListBox().getItemText(
											sv.getYearListBox()
													.getSelectedIndex()),
									sv.getVersionListBox().getItemText(
											sv.getVersionListBox()
													.getSelectedIndex()),
									new AsyncCallback<List<HashMap<String, String>>>() {

										@Override
										public void onFailure(Throwable caught) {
											// TODO Auto-generated method stub

										}

										@Override
										public void onSuccess(
												List<HashMap<String, String>> result) {
											center.clear();
											for (HashMap<String, String> film : result) {
												center.add(new SearchResultView(
														film.get("filmName"),
														film.get("year"),
														"["
																+ film.get("version")
																+ "]",
														new String(
																film.get("actor0")
																		+ ", "
																		+ film.get("actor1")
																		+ ", "
																		+ film.get("actor2")),
														film.get("producerName"),
														film.get("directorName"),
														film.get("date"),
														film.get("duration"),
														film.get("synopsis"),
														film.get("affiche"),
														film.get("rating")
																+ "/10"));

											}
										}
									});

				}

			}
		});

		north2.add(sv);

		Image boxOffice = new Image("images/box-office.png");
		boxOffice.setTitle("Box Office");
		Image comingSoon = new Image("images/coming-soon.png");
		comingSoon.setTitle("Prochainement");
		Image cinema = new Image("images/cinema.png");
		cinema.setTitle("En Salle");
		HorizontalPanel centerLink = new HorizontalPanel();
		centerLink.setSpacing(15);
		centerLink.add(boxOffice);
		centerLink.add(comingSoon);
		centerLink.add(cinema);
		// center.add(centerLink);
		center.add(new Label());

		boxOffice.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent ev) {
				center.clear();
				System.out.println("Box Office");
			}
		});

		comingSoon.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent ev) {
				center.clear();
				System.out.println("Coming Soon");
			}
		});

		cinema.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent ev) {
				center.clear();
				System.out.println("In cinema");
			}
		});

		// zone footer
		south1.add(new Footer());
		return mainPanel;

	}
}
